﻿
using System.Net.NetworkInformation;
using System.Text;

namespace AppServer
{
    public enum ImageFotmat
    {
        unknown,bmp,gif,png, jpeg, jpeg2,tiff,tiff2
    }

    public interface IImageWriter
    {
        Task<string> UploadImage(IFormFile file,string saveFilePath);
        Task<string> WriteFile(IFormFile file, string saveFilePath);
    }

    public class ImageWriter : IImageWriter
    {
        public async Task<string> UploadImage(IFormFile file, string saveFileName)
        {
            if (CheckIfImageFile(file))
            {
                return await WriteFile(file, saveFileName);
            }
            return "Invalid image file";
        }

        private bool CheckIfImageFile(IFormFile file)
        {
            byte[] fileBytes;
            using (var ms = new MemoryStream())
            {
                file.CopyTo(ms);
                fileBytes = ms.ToArray();
            }
            return GetImageFormat(fileBytes) != ImageFotmat.unknown;
        }

        public async Task<string> WriteFile(IFormFile file,string saveFilePath)
        {
            try
            {
                var extentions = new StringBuilder(".")
                    .Append(file.FileName.Split('.')[file.FileName.Split('.').Length - 1]);

                //string genre = movies.GetMovieGenre(movieName);
                //TextureSaver.SaveVideoTexture(movieId, genre, cover);
                GameDebug.Log($"svae file path:{saveFilePath}");

                using(var bits = new FileStream(saveFilePath, FileMode.Create))
                {
                    await file.CopyToAsync(bits);
                }
            }
            catch(Exception e)
            {
                return e.Message;
            }
            return "success";
        }

        internal static ImageFotmat GetImageFormat(byte[] fileBytes) {
            var bmp = Encoding.ASCII.GetBytes("BM");
            var gif = Encoding.ASCII.GetBytes("GIF");
            var png = new byte[] { 137,80, 78, 71 };//header bytes
            var tiff = new byte[] { 73,73, 42 };//header bytes
            var tiff2 = new byte[] { 77,77,42 };//header bytes
            var jpeg = new byte[] { 255,216,255,224 };//
            var jpeg2 = new byte[] { 255, 216,255, 225 };//header bytes

            if (bmp.SequenceEqual(fileBytes.Take(bmp.Length)))
                return ImageFotmat.bmp;
            if (gif.SequenceEqual(fileBytes.Take(gif.Length)))
                return ImageFotmat.gif;
            if (png.SequenceEqual(fileBytes.Take(png.Length)))
                return ImageFotmat.png;
            if (tiff.SequenceEqual(fileBytes.Take(tiff.Length)))
                return ImageFotmat.tiff;
            if (tiff2.SequenceEqual(fileBytes.Take(tiff2.Length)))
                return ImageFotmat.tiff2;
            if (jpeg.SequenceEqual(fileBytes.Take(jpeg.Length)))
                return ImageFotmat.jpeg;
            if (jpeg2.SequenceEqual(fileBytes.Take(jpeg2.Length)))
                return ImageFotmat.jpeg2;

            return ImageFotmat.unknown;
        }
    }

}